home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CSimpleUDPServer.cp < prev    next >
Encoding:
Text File  |  1998-08-10  |  5.8 KB  |  202 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CSimpleUDPServer.cp            ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    Servers are endpoints that watch and wait for connection requests from
  5. //    remote clients. The actual connection to and interaction with remote clients
  6. //    is handled by the "responders."
  7.  
  8. #include "CSimpleUDPServer.h"
  9.  
  10. #include <UNetworkFactory.h>
  11. #include <LWindow.h>
  12. #include <LApplication.h>
  13. #include <UMemoryMgr.h>
  14.  
  15.  
  16. // ===========================================================================
  17. //        • Resource IDs
  18. // ===========================================================================
  19.  
  20. const PP_PowerPlant::ResIDT        PPob_UDPTerminalWindow    = 130;
  21. const PP_PowerPlant::PaneIDT    pPort                    = 'PORT';
  22. const PP_PowerPlant::PaneIDT    pAddress                = 'ADDR';
  23. const PP_PowerPlant::PaneIDT    pTerminal                = 'TERM';
  24.  
  25. // ---------------------------------------------------------------------------
  26. //        • CSimpleUDPServer
  27. // ---------------------------------------------------------------------------
  28.  
  29. CSimpleUDPServer::CSimpleUDPServer(
  30.         LCommander* inSuper)
  31.         : LSingleDoc(inSuper)
  32. {
  33.     mWindow = nil;
  34.     mEndpoint = nil;
  35.     mServerThread = nil;
  36.     mQuitWhenDone = false;
  37.     mSaveOption = 0;
  38. }
  39.  
  40. // ---------------------------------------------------------------------------
  41. //        • ~CSimpleUDPServer
  42. // ---------------------------------------------------------------------------
  43.  
  44. CSimpleUDPServer::~CSimpleUDPServer()
  45. {
  46.     if (mEndpoint) {
  47.         delete mEndpoint;
  48.         mEndpoint = nil;
  49.     }
  50.  
  51.     if (mServerThread)
  52.         mServerThread->DeleteThread();
  53. }
  54.  
  55. // ---------------------------------------------------------------------------
  56. //        • WaitForUDPData
  57. // ---------------------------------------------------------------------------
  58. //    Wait for connection(s) from remote machine(s) using the parameters that have
  59. //    been passed to us. The connection information window is also opened at this time.
  60. //
  61. //    This routine creates an endpoint and a thread for the server process.
  62. //    When the thread runs, it will bind the endpoint and wait for T_Listen events.
  63. //    Responses to T_Listens will be handled on NEW endpoints. This endpoint ONLY listens.
  64.  
  65. void
  66. CSimpleUDPServer::WaitForUDPData(
  67.     UInt32        inPort)
  68. {
  69.     CreateServerWindow(inPort);
  70.     
  71.     mEndpoint = PP_PowerPlant::UNetworkFactory::CreateUDPEndpoint();
  72.  
  73.     mServerThread = new CUDPServerThread(
  74.                             inPort,
  75.                             mEndpoint,
  76.                             this,
  77.                             mTerminalPane);
  78.  
  79.     mServerThread->Resume();
  80. }
  81.  
  82. // ---------------------------------------------------------------------------
  83. //        • ServerThreadDied
  84. // ---------------------------------------------------------------------------
  85.  
  86. void
  87. CSimpleUDPServer::ServerThreadDied()
  88. {
  89.     mServerThread = nil;
  90.  
  91.     //Re-issue the quit command when we are ready.
  92.     if (mQuitWhenDone) {
  93.         PP_PowerPlant::LApplication* theApp = dynamic_cast<PP_PowerPlant::LApplication*>(GetSuperCommander());
  94.         if ( theApp ) {
  95.             theApp->DoQuit(mSaveOption);
  96.         }
  97.     }
  98.  
  99.     delete this;
  100. }
  101.  
  102. PP_PowerPlant::LUDPEndpoint* 
  103. CSimpleUDPServer::GetEndPoint() const
  104. {
  105.     return mEndpoint;
  106. }
  107.  
  108. // ---------------------------------------------------------------------------
  109. //        • CreateServerWindow
  110. // ---------------------------------------------------------------------------
  111.  
  112. void
  113. CSimpleUDPServer::CreateServerWindow(
  114.     UInt32        inPort)
  115. {
  116.     PP_PowerPlant::LCaption* theField;
  117.  
  118.     mWindow = PP_PowerPlant::LWindow::CreateWindow(PPob_UDPTerminalWindow, this);
  119.  
  120.     mTerminalPane = dynamic_cast<CTerminalPane*> (mWindow->FindPaneByID(pTerminal));
  121.     ThrowIfNil_ (mTerminalPane);
  122.     
  123.     theField = dynamic_cast<PP_PowerPlant::LCaption*> (mWindow->FindPaneByID(pPort));
  124.     ThrowIfNil_ (theField);
  125.     theField->SetValue(inPort);
  126.     
  127.     theField = dynamic_cast<PP_PowerPlant::LCaption*> (mWindow->FindPaneByID(pAddress));
  128.     ThrowIfNil_ (theField);
  129.     
  130.     PP_PowerPlant::StDeleter<PP_PowerPlant::LInternetMapper>
  131.                         theMapper(PP_PowerPlant::UNetworkFactory::CreateInternetMapper());
  132.  
  133.     PP_PowerPlant::StDeleter<PP_PowerPlant::LInternetAddress>
  134.                         tempAddress(theMapper->GetLocalAddress());
  135.  
  136.     Str255    tempDescriptor;
  137.     theField->SetDescriptor(tempAddress->GetIPDescriptor(tempDescriptor));
  138. }
  139.  
  140. // ---------------------------------------------------------------------------
  141. //        • BindCompleted
  142. // ---------------------------------------------------------------------------
  143. //    We are now waiting for T_Listens... show the connection window
  144.  
  145. void
  146. CSimpleUDPServer::BindCompleted()
  147. {
  148.     mWindow->Show();
  149. }
  150.  
  151. // ---------------------------------------------------------------------------
  152. //        • BindFailed
  153. // ---------------------------------------------------------------------------
  154. //    Could not bind to the requested port.
  155.  
  156. void
  157. CSimpleUDPServer::BindFailed()
  158. {
  159.     SignalPStr_("\pBind Failed");
  160.     delete this;
  161. }
  162.  
  163. // ---------------------------------------------------------------------------
  164. //        • AllowSubRemoval
  165. // ---------------------------------------------------------------------------
  166. //    If user tries to close the main window, and we have ongoing connections,
  167. //    then block the action. If we don't have any connections then
  168. //    interpret this as a request to close the server.
  169.  
  170. Boolean
  171. CSimpleUDPServer::AllowSubRemoval(
  172.     LCommander *inSub)
  173. {
  174.     if (inSub == mWindow) {
  175.         mServerThread->Complete();
  176.         return false;
  177.     }
  178.     else
  179.         return LSingleDoc::AllowSubRemoval(inSub);
  180. }
  181.  
  182. // ---------------------------------------------------------------------------
  183. //        • AttemptQuitSelf
  184. // ---------------------------------------------------------------------------
  185. //    Close our connection before quitting. We re-issue the quit command via
  186. //    the ServerThreadDied method once we are done.
  187.  
  188. Boolean
  189. CSimpleUDPServer::AttemptQuitSelf(
  190.     SInt32    inSaveOption)
  191. {
  192.     if (mQuitWhenDone) {
  193.         return LCommander::AttemptQuitSelf(inSaveOption);
  194.     } else {
  195.         mQuitWhenDone = true;
  196.         mSaveOption = inSaveOption;
  197.  
  198.         mServerThread->Complete();
  199.         return false;
  200.     }
  201. }
  202.